package com.mooc.ppjoke.extention;

import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

/**
 * @ClassName: AbsPagedListAdapter$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/25 15:38
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public abstract class AbsPagedListAdapter<T,VH extends RecyclerView.ViewHolder> extends PagedListAdapter<T,VH> {
    private SparseArray<View> mHeaders=new SparseArray<>();
    private SparseArray<View> mFooters=new SparseArray<>();
    private int BASE_ITEM_TYPE_HEADER = 100000;
    private int BASE_ITEM_TYPE_FOOTER = 200000;
    protected AbsPagedListAdapter(@NonNull @NotNull DiffUtil.ItemCallback<T> diffCallback) {
        super(diffCallback);
    }

    public void addHeaderView(View view){
        if(mHeaders.indexOfValue(view)<0){
            mHeaders.put(BASE_ITEM_TYPE_HEADER++, view);
            notifyDataSetChanged();
        }
    }
    public void addFooterView(View view){
        if(mFooters.indexOfValue(view)<0){
            mFooters.put(BASE_ITEM_TYPE_FOOTER++, view);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        int itemCount = super.getItemCount();
        return itemCount+mHeaders.size()+mFooters.size();
    }

    public int getOriginalItemCount(){
        return getItemCount()-mFooters.size()-mHeaders.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(isHeaderPosition(position)){
            //返回该position对应的headerview的viewtype类型
            return mHeaders.keyAt(position);
        }
        if(isFooterPosition(position)){
            position=position-getOriginalItemCount()-mHeaders.size();
            return mFooters.keyAt(position);
        }
        position=position-mHeaders.size();
        return getItemViewType2(position);
    }

    public int getHeaderCount() {
        return mHeaders.size();
    }
    protected int getItemViewType2(int position) {
        return 0;
    }
    public int getFooterCount() {
        return mFooters.size();
    }

    private boolean isFooterPosition(int position) {
        return position>=getOriginalItemCount()+mHeaders.size();
    }

    private boolean isHeaderPosition(int position) {

        return position<mHeaders.size();
    }

    @NonNull
    @NotNull
    @Override
    public VH onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (mHeaders.indexOfKey(viewType) >= 0) {
            View view = mHeaders.get(viewType);
            return (VH) new RecyclerView.ViewHolder(view) {
            };
        }

        if (mFooters.indexOfKey(viewType) >= 0) {
            View view = mFooters.get(viewType);
            return (VH) new RecyclerView.ViewHolder(view) {
            };
        }
        return onCreateViewHolder2(parent,viewType);
    }

    protected abstract VH onCreateViewHolder2(ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(@NonNull @NotNull VH holder, int position) {
        if (isHeaderPosition(position) || isFooterPosition(position))
        return;
        //列表中正常类型的itemView的 position 咱们需要减去添加headerView的个数
        position = position - mHeaders.size();
        onBindViewHolder2(holder, position);
    }

    protected abstract void onBindViewHolder2(VH holder, int position);

    @Override
    public void onViewAttachedToWindow(@NonNull @NotNull VH holder) {
        if (!isHeaderPosition(holder.getAdapterPosition()) && !isFooterPosition(holder.getAdapterPosition())) {
            this.onViewAttachedToWindow2((VH) holder);
        }
    }
    public void onViewAttachedToWindow2(VH holder) {

    }
    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        if (!isHeaderPosition(holder.getAdapterPosition()) && !isFooterPosition(holder.getAdapterPosition())) {
            this.onViewDetachedFromWindow2((VH) holder);
        }
    }

    public void onViewDetachedFromWindow2(VH holder) {

    }
    @Override
    public void registerAdapterDataObserver(@NonNull @NotNull RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(new MyAdapterDataObserver(observer));
    }

    // 移除头部
    public void removeHeaderView(View view) {
        int index = mHeaders.indexOfValue(view);
        if (index < 0) return;
        mHeaders.removeAt(index);
        notifyDataSetChanged();
    }

    // 移除底部
    public void removeFooterView(View view) {
        int index = mFooters.indexOfValue(view);
        if (index < 0) return;
        mFooters.removeAt(index);
        notifyDataSetChanged();
    }

    private class MyAdapterDataObserver extends RecyclerView.AdapterDataObserver{
        private RecyclerView.AdapterDataObserver observer;

        public MyAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
            this.observer = observer;
        }

        @Override
        public void onChanged() {
            // Do nothing
            observer.onChanged();
        }
        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            // do nothing
            observer.onItemRangeChanged(positionStart+mHeaders.size(), itemCount);
        }
        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {

            observer.onItemRangeChanged(positionStart+mHeaders.size(), itemCount,payload);
        }
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            // do nothing
            observer.onItemRangeInserted(positionStart+mHeaders.size(), itemCount);
        }
        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            // do nothing
            observer.onItemRangeRemoved(positionStart+mHeaders.size(), itemCount);
        }
        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            observer.onItemRangeMoved(fromPosition+mHeaders.size(),toPosition+mHeaders.size(), itemCount);
            // do nothing
        }
    }
}
